package com.example.myapplication.vo;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import com.example.myapplication.until.Globals;
import com.example.myapplication.until.ImageUtils;
import com.example.myapplication.until.JBoxUtils;
import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import java.util.Iterator;
import java.util.Random;

public class Bird extends Item{

    private static int BIRDINDEX=0 ;

    private boolean toAdd=false ;
    private float r;
    private int type;
    private int bridIndex ;
    private boolean birdCenterFlag=false ;

    public Bird(int type,int index) {
        switch (type) {
            case 1:
                r = Globals.PIECE_WIDTH / 2;
                break;
        }
        bridIndex = index ;
        this.type = type;
    }

    public static void drewBirds(Canvas canvas,Paint paint, Point p){
        Iterator iterator = Globals.BIRDLIST.iterator();
        while (iterator.hasNext()){
            Bird bird = (Bird) iterator.next() ;
            //bird.add();
            if(bird.isDead()){
                iterator.remove();
            }else{
                bird.drawItem(canvas, paint, p);
            }
        }
    }

    public static int getBridIndex(){
        return (BIRDINDEX++)%4;
    }

    public static int getBridCuerrentIndex(){
        return BIRDINDEX%4;
    }

    public static int getPreBridIndex(){
        return BIRDINDEX%4;
    }

    public void setCenter(boolean ok){
        birdCenterFlag = ok ;
    }

    public int getIndex() {
        return bridIndex;
    }

    @Override
    public void drawItem(Canvas canvas, Paint paint, Point nowPosition) {
        canvas.save();
        canvas.rotate((float) (body.getAngle() * 180 / Math.PI),
                body.getPosition().x * Globals.RATE + nowPosition.x,
                body.getPosition().y * Globals.RATE + nowPosition.y);

        canvas.drawBitmap(ImageUtils.getBirdImg(bridIndex), body.getPosition().x
                * Globals.RATE - r + nowPosition.x, body.getPosition().y
                * Globals.RATE - r + nowPosition.y, paint);

        Vec2 v = body.getPosition() ;

        //System.out.println(Globals.birdCenterFlag);

        if (birdCenterFlag) {
            // 保证正在飞行的鸟的坐标在屏幕正中
            nowPosition.x = (int) -(body.getPosition().x * Globals.RATE - Globals.SCREEN_WIDTH / 2);
            nowPosition.y = (int) -(body.getPosition().y * Globals.RATE - Globals.SCREEN_HEIGH / 2);

            if (nowPosition.x > 0) {
                nowPosition.x = 0;
            }
            if (nowPosition.y > 0) {
                nowPosition.y = 0;
            }
            if (nowPosition.x < -Globals.SCREEN_WIDTH) {
                nowPosition.x = -Globals.SCREEN_WIDTH;
            }
            if (nowPosition.y < -Globals.SCREEN_HEIGH) {
                nowPosition.y = -Globals.SCREEN_HEIGH;
            }
        }
        canvas.restore();
    }

    @Override
    public void hit(Object o, float li , ContactImpulse arg) {
        setLife(getLife() - li / 10);
        //type1();
    }

//    private void type1(){
//        toAdd = true ;
//    }
//
//    public void add(){
//        if(toAdd&&body!=null){
//            for (int i = 0; i < 3; i++) {
//                Bird bird = new Bird(1,(new Random().nextInt())%2) ;
//                Body b = JBoxUtils.createCircle(10,body.getPosition().x,body.getPosition().y,JBoxUtils.BIRD_TYPE,(float) (Math.PI/4*i),false) ;
//                bird.setBody(b);
//                Vec2 v = new Vec2(1000,1000) ;
//                body.applyLinearImpulse(v,body.getWorldCenter());
//                Globals.BIRDLIST.add(bird);
//            }
//            toAdd=false ;
//        }
//    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
        this.body.m_userData = this;
    }
}